clear all
set type double
version 18.5
set seed 10
sysdir set PLUS ado_rdrobust920/plus

log using "log_filename.log", replace

* run this first *
display "$S_TIME  $S_DATE"
do modules/_init.do

* output: 
* note: create folders tmp, tables, and graphs

***************************
**** data construction ****
***************************
display "$S_TIME  $S_DATE"
do Step1_screening.do	/* */
* Ado-package (bundled) : sysdir set PLUS ado_rdrobust760/plus
* output:
*	1) "firmleveltest/test_sample_with_results.dta", needed to do clustering analysis, 
*	2) "firmleveltest/graph_sp1x2/...pdf", scatter plots for for tested 1143 firms 
* note: Implements cartel-screening for each firm. Outputs are .dta files and pdf graphics. 
* It takes long time (8 - 10 hours)

display "$S_TIME  $S_DATE"
do Step2_clustering.do
* output: "firmleveltest/ringfirms_plus.dta", used in "Step4_treatment_assignment.do"
* note: Classifies firms into 26 groups using a clustering algorithm.

display "$S_TIME  $S_DATE"
do Step3_treatment_assignment.do
* output: "fishertest/group_treatment.dta, used in fisher test
* note: treatment status is assigned here.

display "$S_TIME  $S_DATE"
do Step4_data_construction1.do
* output: "tmp/temp_biddata_090122.dta"
* note: Construct the sample for the main analysis using biddata090122.dta, which  
* contains bid data from fiscal years 2013 through 2020, including license id and firm head-
* quarter addresses. If the address is missing in biddata090122.dta, We supplement it using 
*  address_license_121818.csv, which is the list of all construction firms as of year 2018.

display "$S_TIME  $S_DATE"
do Step5_data_construction2.do
* output: "tmp/sample_090122.dta"
* note: Finalizes the dataset used in the main analysis. 


***************************
****** main analysis ******
***************************
*Note: data constructnion codes from Steps 1 through 5 must run before running the following codes

/* summary stats*/
display "$S_TIME  $S_DATE"
do modules/_summary_stat_table.do
* output: "table/summary_stats_auctions.tex", Table 2 Summary Statistics (Auctions)
display "$S_TIME  $S_DATE"
do modules/_summary_stat_table_firm.do
* output: "table/summary_stats_firms.tex", Table 3 Summary Statistics (Firms)

/* fdr analysis */
display "$S_TIME  $S_DATE"
do modules/_fdr.do
* output: 
*	1) "figures/fdr_noshrinkage_cdfplot_ftt_ts1s.png", Figure 3 Empirical CDF of t statistics
*	2) "figures/fdr_noshrinkage_cdfplot_ftt_ts1p.png", Figure OA5 in online Appendix

/* twoway time series */
display "$S_TIME  $S_DATE"
do modules/_time_series_plot.do
* output:
*	1) "graph/twowaylpoly_abovereserve_timeseries.png" 				Figure 9  
*	2) "graph/twowaylpoly_pctlosingbids_timeseries.png"				Figure 8 left 
*	3) "graph/twowaylpoly_winbidpct_timeseries.png"					Figure 8 right
*	4) "graph/twowaylpoly_q_winners_timeseries.png"					Figure 10 right
*	5) "graph/twowaylpoly_q_losers_timeseries.png"					Figure 10 left


** Draw one-sided RD graph **
display "$S_TIME  $S_DATE"
do modules/_draw_main_scatter_plots.do
* output:
*	1) graph/Treatmentfirms_s_by31mar2021_condition_15mar2019.png	Figure4 Top
*	2) graph/Controlfirms_s_by31mar2021_condition_15mar2019.png		Figure4 Bottom
*	3) graph/Controlfirms_p_by31mar2021_condition_15mar2019.png		Figure5 Bottom
*	4) graph/Treatmentfirms_p_by31mar2021_condition_15mar2019.png	Figure5 Top



**** Draw graphcs on Nippo Co.ltd ****
display "$S_TIME  $S_DATE"
do modules/_nippo.do
* output: 
*	1) "graph/NIPPOp.png"		Figure 2
* 	2) "graph/NIPPO.png"		Figure 1
*	3) "tables/nippo.tex"		Table 1


*** fisher test ***
display "$S_TIME  $S_DATE"
do modules/_fisher_test.do	/*takes 20-30 mins*/
* output: 
*	1) "graph/fisher_scatter__auction_mean.png" 				Figure 7
*	2) "graph/diffdiff_abovereserve_l_bidder_mean.png"			Figure 12
* 	3) "graph/diffdiff_pctbid_w_pctbid_l_bidder_mean.png"		Figure 11 left 
*	4) "graph/diffdiff_q_w_q_l_bidder_mean.png"					Figure 11 right
*	5) "graph/diffdiff_tau_score_tau_price_auction_mean.png" 	Figure 6


***************************
*** for online appendix ***
***************************

/* Draw one-sided RD graph (bin scatter)*/
display "$S_TIME  $S_DATE"
do modules/_binscatter_form_main_plots.do
* output:
*	1) "graph/binscatter_Treatmentfirms_s_by.png",				Figure OB.1 Top
*	2) "graph/binscatter_Controlfirms_s_by.png",				Figure OB.1 Bottom
*	3) "graph/binscatter_Treatmentfirms_p_by.png"				Figure OB.2 Top
*	4) "graph/binscatter_Controlfirms_p_by.png"					Figure OB.2 Bottom


/*event study format*/
display "$S_TIME  $S_DATE"
do modules/_time_series_event_study_form.do
* output: 
*	1) "graph/event_twowaylpoly_pctlosingbids_timeseries.png"	Figure OB.3 right
*	2) "graph/event_twowaylpoly_winbidpct_timeseries.png"		Figure OB.3 left
*	3) "graph/event_twowaylpoly_q_losers_timeseries.png"		Figure OB.5 right
*	4) "graph/event_twowaylpoly_q_winners_timeseries.png"		Figure OB.5 left
*	5) "graph/event_twowaylpoly_abovereserve_timeseries.png"	Figure OB.4


** create distance matrix by group **
* requires "../firmleveltest/test_sample_with_results.dta" created by Step1_screening.do
display "$S_TIME  $S_DATE"
do modules/_distance_matrix_groupwise.do
distance_matrix_groupwise
* output:
*	1) "firmleveltest/freq_matrix28_Eastern_Japan.tex			Table OA.1 middle
*	2) "firmleveltest/freq_matrix28_Hokkaido.tex				Table OA.1 Top
*	3) "firmleveltest/freq_matrix28_Western_Japan.tex			Table OA.1 Bottom


** classification table ***
* requires "../firmleveltest/test_sample_with_results.dta" created by Step1_screening.do
display "$S_TIME  $S_DATE"
do modules/_classification_table.do
* output: 
*	1) "tables/classification1.csv"		Table in the end of OA
*	2) "tables/classification2.csv"		Table in the end of OA
*	3) "tables/classification3.csv"		Table in the end of OA
*	4) "tables/classification4.csv"		Table in the end of OA
*	5) "tables/classification5.csv"		Table in the end of OA
*	6) "tables/classification6.csv"		Table in the end of OA
*	7) "tables/classification7.csv"		Table in the end of OA
*	8) "tables/classification8.csv"		Table in the end of OA
*	9) "tables/classification9.csv"		Table in the end of OA
*	10) "tables/classification10.csv"	Table in the end of OA

display "$S_TIME  $S_DATE"

log close
